/** \file Singleton_t.cc 
 * Test definitions for the Singleton class template.
 *
 * (c) Copyright  Semih Cemiloglu
 * All rights reserved, see COPYRIGHT file for details.
 *
 * $Id: Singleton_t.cc 119 2006-06-21 04:57:25Z semihc $
 *
 *
 */

#include "Singleton_t.hh"
#include <QtTest/QtTest>
#include "Singleton.hpp"
#include "SingletonMgr.hh"

using namespace std;
using namespace TC;

TC_IDENT("$Id$");

namespace {
  class A 
  {
    friend class ::TC::SingletonMgr;
  public:
    int m_val;
  protected:
    A() : m_val(0) {}
  };


  class B : public A
  {
    friend class ::TC::SingletonMgr;
  public:
    
  private:
    B() { m_val = 1; }
  };

}

void Singleton_t::tests()
{

  // Global singleton test
  SingletonMgr* gmgr = GlobalSingletonMgr();

  Singleton<A>::SetManager(gmgr);

  QVERIFY(Singleton<A>::GetManager()==gmgr);

  Singleton<A>::GetInstance().m_val++;

  Singleton<A>::SetLongevity(2);

  QVERIFY(Singleton<A>::GetLongevity()==2);

  int val = ++(Singleton<A>::GetInstance().m_val);

  QVERIFY(val==2);


  // Thread singleton test
  SingletonMgr* tmgr = ThreadSingletonMgr();


  QVERIFY(gmgr != tmgr);
  try {
    Singleton<A>::SetManager(tmgr);
    QVERIFY(false); // Expect an assertion failure
  } catch(...) {}
  Singleton<B>::SetManager(tmgr);
  QVERIFY(Singleton<B>::GetManager()==tmgr);
  Singleton<B>::GetInstance().m_val++;
  Singleton<B>::SetLongevity(2);
  QVERIFY(Singleton<B>::GetLongevity()==2);
  val = ++(Singleton<B>::GetInstance().m_val);
  QVERIFY(val==3);

}

  



